package com.chockqiu.html.fontface;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

import java.util.HashMap;
import java.util.WeakHashMap;

public abstract class HtmlFontFaceHandler implements HtmlTagHandler {

    public String mTagName = "kfont";
    public static WeakHashMap<String, Typeface> mTypefaceCache = new WeakHashMap<>();

    public HtmlFontFaceHandler() {
    }

    public HtmlFontFaceHandler(String tagName) {
        mTagName = tagName;
    }

    private int startIndex = 0;

    @Override
    public void handleTag(boolean opening, String tag, Editable output, HashMap<String, String> attributes) {
        if (tag.equalsIgnoreCase(mTagName)) {
            if (opening) {
                startFont(tag, output, attributes);
            } else {
                endFont(tag, output, attributes);
            }
        }
    }

    public abstract Typeface onNeedFontFace(String faceName);

    @Override
    public String getTagName() {
        return mTagName;
    }

    public void startFont(String tag, Editable output, HashMap<String, String> attributes) {
        startIndex = output.length();
    }

    public void endFont(String tag, Editable output, HashMap<String, String> attributes) {
        try {
            int stopIndex = output.length();
            String color = attributes.get("color");
            String size = attributes.get("size");
            String face = attributes.get("face");
            if (face != null && !TextUtils.isEmpty(face)) {
                Typeface mTypeface = mTypefaceCache.get(face);
                if (mTypeface == null) {
                    try {
                        mTypeface = onNeedFontFace(face);
                        if (mTypeface != null) {
                            mTypefaceCache.put(face, mTypeface);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (mTypeface != null) {
                    output.setSpan(new TypefaceSpan(mTypeface), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                } else {
                    Log.e("HtmlFontFaceHandler", "face '" + face + "' not found");
                }
            }
            if (size != null) {
                if (size.contains("px")) {
                    size = size.split("px")[0];
                } else if (size.contains("dp")) {
                    size = size.split("dp")[0];
                } else if (size.contains("sp")) {
                    size = size.split("sp")[0];
                }
            }
            if (!TextUtils.isEmpty(color) && !TextUtils.isEmpty(size)) {
                output.setSpan(new ForegroundColorSpan(Color.parseColor(color)), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (size != null && !TextUtils.isEmpty(size)) {
                output.setSpan(new AbsoluteSizeSpan(Integer.parseInt(size), true), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}